<?php

namespace app\mobile\controller;


use org\Sitemap;
use Yunpian\Sdk\YunpianClient;

class Index extends Base
{

    public function sns(){
        //初始化client,apikey作为所有请求的默认值
        $clnt = @YunpianClient::create();

        $param = [YunpianClient::MOBILE => '18616020000',YunpianClient::TEXT => '【云片网】您的验证码是1234'];
        $r = $clnt->sms()->single_send($param);
dump($clnt->user());
        if($r->isSucc()){
            //$r->data()
        }
    }

    public function index($navid = null)
    {
        /*
         * 商品栏目
        $product_nav = cache('product_nav' . cookie('think_var'));
        if (empty($product_nav)) {
            $product_nav = db('product_nav')
                ->where('status', '1')
                ->select();
            foreach ($product_nav as $key => $value) {
                $value['type'] = trim($value['type'], ",");
                $ayy = explode(',', $value['type']);
                $o = array_filter($ayy);
                $product_nav[$key]['ling'] =array();
                foreach ($o as $keyy => $valuee) {
                    $product_nav[$key]['ling'][$keyy]['fname'] = db('nav')->where('id', $valuee)->value('name','id');
                    $product_nav[$key]['ling'][$keyy]['id'] =$valuee;
                    $product_nav[$key]['ling'][$keyy]['fname_nav'] = db('nav')
                        ->where('pid', $valuee)
                        ->where('model', 'Product')
                        ->where('status', '1')
                        ->order('sort desc')
                        ->select();
                }
            }
            cache('product_nav' . cookie('think_var'), $product_nav);
        }
        */

        //友情链接
        $link = cache('link' . cookie('think_var'));
        if (empty($link)) {
            $link = model('link')->where($this->datas)->limit(16)->select();
            cache('link' . cookie('think_var'), $link);
        }

        //视屏广告连接
        $adindex = cache('adindex' . cookie('think_var'));
        if (empty($adindex)) {
            $adindex = model('adindex')->where($this->datas)->order('sort DESC')->find();
            cache('adindex' . cookie('think_var'), $adindex);
        }


        //案例
        $ucase = cache('ucase' . cookie('think_var'));
        if (empty($ucase)) {
            $ucase = db('ucase')->alias('a')
                ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
                ->field('a.*,c.name')
                ->where('a.status', '1')
                ->where('a.lang', cookie('think_var'))
                ->order('a.sort DESC,a.id DESC')
                ->limit(8)
                ->select();
            cache('ucase' . cookie('think_var'), $ucase);
        }

        //推荐产品
        $product = cache('product' . cookie('think_var'));
        if (empty($product)) {
            $product = db('product')
                ->alias('a')
                ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
                ->field('a.*,c.name')
                ->where('a.status', '1')
                ->where('a.tuijian', '1')
                ->where('a.lang', cookie('think_var'))
                ->order('a.sort DESC,a.id DESC')
                ->limit(8)
                ->select();
            cache('product' . cookie('think_var'), $product);
        }

        //产品分类显示
        $product1 = cache('product1' . cookie('think_var'));
        if (empty($product1)) {
            $product1 = db('nav')
                ->field('name,id,model')
                ->where('status', '1')
                ->where('model', 'product')
                ->where('lang', cookie('think_var'))
                ->order('sort DESC')
                ->select();

            foreach ($product1 as $key => $value) {
                $product1[$key]['list'] = db('product')
                    ->field('title,id,cid,miaoshu,image')
                    ->where('cid', $value['id'])
                    ->where('status', '1')
                    ->where('lang', cookie('think_var'))
                    ->order('sort DESC')
                    ->limit(3)
                    ->select();
                if (empty($product1[$key]['list'])) {
                    unset($product1[$key]);
                }
            }
            cache('product1' . cookie('think_var'), $product1);

        }
        //新闻动态
        $Article = cache('Article' . cookie('think_var'));
        if (empty($Article)) {
            $Article = db('Article')
                ->alias('a')
                ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
                ->field('a.id,a.issue,a.miaoshu,a.title,a.image,a.cid,c.name,c.create_time')
                ->where('a.status', '1')
                ->where('a.lang', cookie('think_var'))
                ->order('a.sort DESC,a.id DESC')
                ->limit(8)
                ->select();
            cache('Article' . cookie('think_var'), $Article);
        }

        //信息学堂
        $classroom = cache('classroom'.cookie('think_var'));
        if (empty($classroom)) {
            $classroom = db('classroom')
                ->alias('a')
                ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
                ->field('a.*,c.name')
                ->where('a.status', '1')
                ->where('a.lang', cookie('think_var'))
                ->order('a.sort DESC,a.id DESC')
                ->limit(8)
                ->select();
            cache('classroom'.cookie('think_var'), $classroom);
        }

        $this->assign([
            'link' => $link,
            'ucase' => $ucase,
            'product' => $product,
            'adindex' => $adindex,
            'classroom' => $classroom,
            'product1' => $product1,
            'Article' => $Article,
//            'product_nav' => $product_nav,
        ]);
        return $this->fetch($this->template . 'index.html');

    }

    public function lang()
    {
        switch ($this->request->param('lang')) {
            case 'en':
                cookie('think_var', 'en-us');
                break;
            case 'cn':
            default:
                cookie('think_var', 'zh-cn');
                break;
            //其它语言
        }
//        $this->redirect($this->request->server('HTTP_REFERER'));
    }

    public function setmap(){
        $Article = db('Article')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.sort DESC,a.id DESC')
            ->select();
        $classroom = db('classroom')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.sort DESC,a.id DESC')
            ->select();
        $nav = db('nav')
            ->field('id,model,create_time')
            ->where('status', '1')
            ->where('model', 'about')
            ->order('sort DESC,id DESC')
            ->select();
        $product = db('product')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.sort DESC,a.id DESC')
            ->select();
        $ucase = db('ucase')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.sort DESC,a.id DESC')
            ->select();
        $all = array_merge($Article,$classroom,$product,$ucase,$nav);
        $site = new Sitemap();
        foreach ($all as $v)
        {
            if(empty($v['cid'])){
                $dd['url']= $this->request->domain().url($v['model'].'/index',['navid'=>$v['id']]);
            }else{
                $dd['url']= $this->request->domain().url($v['model'].'/detailed',['id'=>$v['id'],'navid'=>$v['cid']]);
            }
            $dd['time']= date('Y-m-d H:i:s', $v['create_time']);

            $site->AddItem($dd,1);
        }
        $site->SaveToFile('sitemap.xml');
        $this->redirect($this->request->server('HTTP_REFERER'));

    }
}